from .base import ReserveSql
from ..utils.token import generate_password_hash, check_password_hash, user_token


class UserSql(ReserveSql):
    def __init__(self):
        ReserveSql.__init__(self, 'user')

    def make_token(self, username, password):
        user = self.first(username=username).value('uid', 'username', 'password')
        return {'message': 'The account or password is incorrect'} \
            if not user else {'access_token': user_token(uid=user['uid'], ex=3600 * 4)} \
            if check_password_hash(user['password'], password) else {'message': 'The account or password is incorrect'}
